package com.ssy.invoicing.order.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.stereotype.Repository;

import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.ssy.invoicing.dao.support.BaseSqlDao;
import com.ssy.invoicing.order.bean.SaleOrderBean;
import com.ssy.invoicing.order.bean.SaleProductBean;

@Repository(value = "orderDao")
public class OrderDao extends BaseSqlDao {

    /**
     * 插入订单
     * @author suoyao.shen
     * @param order
     * @return
     */
    public SaleOrderBean saveOrder(SaleOrderBean order) {
        insert("order.insertOrder", order);
        return order;
    }

    /**
     * 批量插入产品
     * 
     * @author wb_suoyao.shen
     * @param saleProductList
     */
    @SuppressWarnings("rawtypes")
    public void saveOrderProductBatch(final List<SaleProductBean> saleProductList) {
        executeWithListResult(new SqlMapClientCallback() {
            private final List<SaleProductBean> productList = saleProductList;

            @Override
            public Object doInSqlMapClient(SqlMapExecutor sqlmapexecutor) throws SQLException {
                //开始批量操作
                sqlmapexecutor.startBatch();
                for (SaleProductBean product : productList) {
                    sqlmapexecutor.insert("order.insertProduct", product);
                }
                sqlmapexecutor.executeBatch();
                return productList;
            }
        });
    }

    /**
     * 查询销售订单列表
     * @author suoyao.shen
     * @param params
     * @param page
     * @return
     * @throws SQLException
     */
    public SplitPageResult queryOrderList(Map<String, Object> params, int page) throws SQLException {
        return queryListByPagination("order.queryOrderList", "order.queryOrderCount",
                params, page);
    }
    
    /**
     * 查询没有生成采购订单的销售订单列表
     * @author suoyao.shen
     * @param params
     * @param page
     * @return
     * @throws SQLException
     */
    @SuppressWarnings("unchecked")
    public List<SaleOrderBean> queryOrderListNoPurchase(Map<String, Object> params) throws SQLException {
        return (List<SaleOrderBean>) queryForList("order.queryOrderListNoPurchase", params);
    }
    
    /**
     * 根据销售订单Id加载产品 
     * @author suoyao.shen
     * @param orderId
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<SaleProductBean> loadProductByOrderId(Integer orderId) {
        return (List<SaleProductBean>) queryForList("order.queryProductByOrderId", orderId);
    }
    
    /**
     * 根据销售订单加载没有创建采购订单的产品
     * @param orderId
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<SaleProductBean> loadNoPurchaseProductByOrderId(Integer orderId) {
        return (List<SaleProductBean>) queryForList("order.loadNoPurchaseProductByOrderId", orderId);
    }
    
    /**
     * 查询销售订单详情
     * @param id
     * @return
     */
    public SaleOrderBean getOrderDetail(Integer id) {
        return (SaleOrderBean) queryForObject("order.getOrderDetail", id);
    }
    
    /**
     * 删除产品信息
     * @author suoyao.shen
     * @param productId
     */
    public void removeProduct(List<Integer> productId) {
        delete("order.removeProduct", productId);
    }
    
    /**
     * 查询订单号的数量
     * @param orderNumber
     * @return
     */
    public Integer queryOrderNumberCount(String orderNumber) {
        return (Integer) queryForObject("order.queryOrderNumberCount", orderNumber);
    }
    
    /**
     * 根据订单号删除订单号下的所有产品
     * @param orderId
     */
    public void removeProductByOrderId(Integer orderId) {
        delete("order.removeProductByOrderId", orderId);
    }
    
    /**
     * 根据id删除订单
     * @param orderId
     */
    public void removeOrderById(Integer orderId) {
        delete("order.removeOrderById", orderId);
    }
    
    /**
     * 根据订单号查询订单下的产品数量
     * @param orderId
     * @return
     */
    public Integer getProductCountByOrderId(Integer orderId) {
        return (Integer) queryForObject("order.queryProductCountByOrderId", orderId);
    }
    
}
